package com.leyou.cart.controller;

import com.leyou.cart.pojo.Cart;
import com.leyou.cart.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CartController {
    @Autowired
    private CartService cartService;

    @PostMapping
    public ResponseEntity<Void> addCart(@RequestBody Cart cart) {
        cartService.addCart(cart);
        return ResponseEntity.ok().build();
    }

    @GetMapping
    public ResponseEntity<List<Cart>> queryCartsByUserId(@RequestParam("userId") Long userId) {
        List<Cart> carts = cartService.queryCartsByUserId(userId);
        if (carts != null && 0 != carts.size()) {
            return ResponseEntity.ok(carts);
        }
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }

    @PutMapping
    public ResponseEntity<Void> updateCart(@RequestParam("userId") Long userId, @RequestParam("skuId") Long skuId) {
        cartService.updateCart(userId, skuId);
        return ResponseEntity.status(HttpStatus.CREATED).build();

    }
       @DeleteMapping
    public ResponseEntity<Void> deleteCart(@RequestParam("userId") Long userId, @RequestParam("skuId") Long skuId) {
        cartService.deleteCart(userId, skuId);
        return ResponseEntity.ok().build();

    }

}
